import axios, { InternalAxiosRequestConfig, AxiosResponse } from 'axios';

const request = axios.create({
    baseUrl: 'http://localhost:8080',
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json'
    }
})

//请求拦截器
request.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = token;
            return config;
        }
        return config;
    },
    (error: any) => {
        return Promise.reject(error)
    }
)
//响应拦截器
request.interceptors.response.use(
    (response: AxiosResponse) => {
        console.log("response", response);
        const { code, data, msg } = response.data;
        if (code === 200) {
            return data;
        } else {
            return Promise.reject(msg)
        }
    },
    (error: any) => {
        return Promise.reject(error)
    }
)

export default request;